﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace KRF_KoreaSimulation
{
	public partial class F_SMLT: Form
	{
		C_CMMN CM = new C_CMMN();
		C_MSSG MG = new C_MSSG();
		C_FILE FL = new C_FILE();

		C_NODE[] ND = new C_NODE[C_CMMN.ND];
		
		int mcnt = 1;	// Message Count

		public F_SMLT()
		{
			InitializeComponent();
			MakeInitialization();
		}

		public void MakeInitialization()
		{
			PB_MAC1.Visible = false;
			PB_MAC2.Visible = false;
			PB_MAC3.Visible = false;
			
			PB_FMC1.Visible = false;
			PB_FMC2.Visible = false;
			PB_FMC3.Visible = false;

			LB_MSG1.Text = "";
			LB_MSG2.Text = "";
			LB_MSG3.Text = "";
			LB_MSG4.Text = "";
			LB_MSG5.Text = "";
			LB_MSG6.Text = "";
			LB_MSG7.Text = "";
			LB_MSG8.Text = "";
			LB_MSG9.Text = "";

			PB_RPT4.Visible = false;
			PB_RPT5.Visible = false;
			PB_RPT6.Visible = false;
			PB_RPT7.Visible = false;
			PB_RPT8.Visible = false;
			PB_RPT9.Visible = false;

			LT_LOGB.Items.Clear();
		}

		private void TM_SRUN_Tick( object sender, EventArgs e )
		{
			//TB_TIME.Text = CM.getTIME( mcnt - 1 );
			executeProgram();
			
			if ( checkEnd() == true )
			{
				TM_SRUN.Enabled = false;
				return;
			}
			
		}

		public bool checkEnd()
		{
			if ( mcnt > C_FILE.fcnt )
				return true;
			else
				return false;
		}

		public void executeProgram()
		{
			string time = CM.getTIME( mcnt - 1 );
			string ndid = CM.getNDID( mcnt - 1 );
			string act1 = CM.getACT1( mcnt - 1 );
			string act2 = CM.getACT2( mcnt - 1 );

			//if ( ChooseCountermeasure( CM.getACT1( mcnt ) ) == -1 )
			//{
			//    MessageBox.Show( MG.F_SMLT_CHSE );
			//    Close();
			//}

			if ( ProgressSimulation( time, act2 ) == -1 )
			{
				if ( act2 == "F" )
					terminate( 0 );
				else
				{
					MessageBox.Show( MG.F_SMLT_PRGS );
					Close();
				}
			}

			AddMessageInListbox( time, ndid, act1, act2 );
			mcnt++;
		}

		public void terminate( int no )
		{
			switch ( no )
			{
				case 0:
					if ( mcnt == C_CMMN.TIME.Count ) 
					{
						mcnt = 1;	
						MessageBox.Show( MG.F_SMLT_FNSH );
						TM_SRUN.Enabled = false;
						MakeInitialization();
					}
					break;
				case 9:
					TM_SRUN.Enabled = false;
					break;					
			}
		}

		public int ChooseCountermeasure( string act1 )
		{
			switch ( act1 )
			{
				case "SE":
					//MessageBox.Show( "SEF" );
					return 0;
				default:
					return -1;
			}

			//return 0;
		}

		public int ProgressSimulation( string time, string act2 )
		{
			//Control PCTB;
			Control LABL;

			string name;
			string ndid = CM.getNDID( mcnt - 1 );

			switch( act2 )
			{
				case "GM":
					name = "LB_MSG" + ndid.ToString();
					LABL = findControlByName( name );
					printMessage( LABL, time, act2 );
					break;
				case "FM":
					PB_SND2.Visible = false;
					PB_CPMN.Visible = true;
					name = "LB_MSG" + ndid.ToString();
					LABL = findControlByName( name );
					printMessage( LABL, time, act2 );
					break;
				case "GR":
				case "SR":
				case "RR":
				case "PR":
				case "VR":
				case "DR":
					name = "LB_MSG" + ndid.ToString();
					LABL = findControlByName( name );
					printMessage( LABL, time, act2 );
					break;
				//case "SR":
				//    name = "LB_MSG" + ndid.ToString();
				//    LABL = findControlByName( name );
				//    transmitData( LABL, act2 );
				//    break;
				//case "RR":
				//    name = "LB_MSG" + ndid.ToString();
				//    LABL = findControlByName( name );
				//    transmitData( LABL, act2 );
				//    break;
				//case "PR":
				//    name = "LB_MSG" + ndid.ToString();
				//    LABL = findControlByName( name );
				//    transmitData( LABL, act2 );
				//    break;
				//case "VR":
				//    name = "LB_MSG" + ndid.ToString();
				//    LABL = findControlByName( name );
				//    transmitData( LABL, act2 );
				//    break;
				//case "DR":
				//    name = "LB_MSG" + ndid.ToString();
				//    LABL = findControlByName( name );
				//    transmitData( LABL, act2 );
				//    break;
				default:
				    return -1;
			}

			return 0;
		}

		public void AddCommandInListBox( string ordr )
		{
			LT_LOGB.Items.Add( "--- " + ordr + " ---" );
		}

		public void AddMessageInListbox( string time, string ndid, string act1, string act2 )
		{
			string fnm1 = CM.getCode1Message( act1 );
			string fnm2 = CM.getCode2Message( act2 );

			LT_LOGB.Items.Add( time + ": [ " + ndid + " ] " + fnm1 + " - " + fnm2 );
		}

		// RSLT_MSG로부터 노드 ID 얻기
		public int getNodeID( SortedList RM, int i )
		{
			return int.Parse( string.Format( "{0}", RM.GetKey( i ) ) );
		}

		// RSLT_MSG로부터 수행 내역 얻기
		public string getAction( SortedList RM, int i )
		{
			return RM.GetByIndex( i ).ToString();
		}

		//public void transmitData( object sender, Boolean B )
		//{
		//    if ( B == true )
		//        ( ( Microsoft.VisualBasic.PowerPacks.LineShape )sender ).BorderWidth = 3;
		//    else
		//        ( ( Microsoft.VisualBasic.PowerPacks.LineShape )sender ).BorderWidth = 1;
		//}

		public void printMessage( object sender, string time, string mssg )
		{
			( ( Label )sender ).Text = time + " : " + mssg;
		}

		public void transmitMAC( object sender, Boolean B )
		{
			if ( sender == null )
			{
				MessageBox.Show( MG.F_SMLT_PB_ERRR );
				Close();
			}

			if ( B == true )
				( ( PictureBox )sender ).Visible = true;
			else
				( ( PictureBox )sender ).Visible = false;
		}

		private void BT_STRT_Click( object sender, EventArgs e )
		{
			AddCommandInListBox( "START" );
			FL.writeResultMessage();
			TM_SRUN.Enabled = true;

			//Control PB;
			//string name = "PB_SND" + "1";
			//PB = findControlByName( name );

			//MessageBox.Show( PB.Name ); 
		}

		private Control findControlByName( string name )
		{
			foreach ( Control c in PN_FILD.Controls ) //assuming this is a Form
			{
				if ( c.Name == name )
					return c; //found
			}
			return null; //not found
		}

		private void BT_EXIT_Click( object sender, EventArgs e )
		{
			AddCommandInListBox( "EXIT" );

			mcnt = 0;
			MakeInitialization();
			TM_SRUN.Enabled = false;
		}

		private void BT_STEP_Click( object sender, EventArgs e )
		{
			AddCommandInListBox( mcnt.ToString() + " STEP" );

			executeProgram();

			if ( checkEnd() == true )
				return;
			
			//TM_SRUN.Enabled = true;
			
			//if ( TM_SRUN.Interval % 500 == 0 )
			//    TM_SRUN.Enabled = false;				
		}

		private void BT_STOP_Click( object sender, EventArgs e )
		{
			AddCommandInListBox( "STOP" );
			TM_SRUN.Enabled = false;
		}
	}
}

